
import listener from '../main/channel-listener/index'
// 在主进程中.
const {
  ipcMain
} = require('electron')

function result (data, message, code) {
  return {
    data: data,
    message: message,
    code: code
  }
}

function success (data, message) {
  return result(data, message, 0)
}

function fail (data, message) {
  return result(data, message, 1)
}

//约定第一个参数为调用方法
ipcMain.on('client-request', (event, arg) => {
  let res = fail('调用异常!')
  if (!arg || !arg.method) {
    event.sender.send(`${arg.method}-reply`, fail('调用参数错误'))
  }
  try {
    res = success(listener[arg.method](arg.param), '调用成功!')
  } catch (e) {
    res = fail(e.message)
  }
  event.sender.send(`${arg.method}-reply`, res)
})


